package com.morrislabs.graph
{
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.FileFilter;
	import flash.net.FileReference;
	import flash.utils.ByteArray;
			
	public class GraphFileIO
	{
		private var DEFAULT_FILE_NAME:String = "mynetwork.xml";
		//File types which we want the user to open
		private var FILE_TYPES:Array = [new FileFilter("XML Files", "*.xml;")];		
	
		//FileReference Class well will use to save data
		private var fr:FileReference;
		
		private var resultCallback:Function;
		
		
		public function GraphFileIO()
		{
		}


		public function doOpen(sourceName:String, callbackWhenComplete:Function):void
		{
			//create the FileReference instance
			fr = new FileReference();

			resultCallback = callbackWhenComplete;
			
			//listen for when they select a file
			fr.addEventListener(Event.SELECT, onFileSelect);

			//listen for when then cancel out of the browse dialog
			fr.addEventListener(Event.CANCEL,onOpenCancel);

			if(sourceName.length > 0) {
				sourceName+=".xml";
				FILE_TYPES.push(new FileFilter(sourceName, sourceName));
			}
			
			//open a native browse dialog that filters for xml files
			fr.browse(FILE_TYPES);
		}

		/************ Browse Event Handlers **************/

		//called when the user selects a file from the browse dialog
		private function onFileSelect(e:Event):void
		{
			//listen for when the file has loaded
			fr.addEventListener(Event.COMPLETE, onLoadComplete);

			//listen for any errors reading the file
			fr.addEventListener(IOErrorEvent.IO_ERROR, onLoadError);

			//load the content of the file
			fr.load();
		}

		//called when the user cancels out of the browser dialog
		private function onOpenCancel(e:Event):void
		{
			trace("File Browse Canceled");
			fr = null;
		}

		/************ Select Event Handlers **************/

		//called when the file has completed loading
		private function onLoadComplete(e:Event):void
		{
			//get the data from the file as a ByteArray
			var data:ByteArray = fr.data;

			//read the bytes of the file as a string and put it in the
			//textarea
			var stext:String = data.readUTFBytes(data.bytesAvailable);
			var xmlData:XML = new XML(stext);
			
			//addEventListener(ResultEvent.RESULT, resultCallback);
			//clean up the FileReference instance

			fr = null;
			
			NthDegrees.getInstance().rebuildGraph(xmlData);
		}

		//called if an error occurs while loading the file contents
		private function onLoadError(e:IOErrorEvent):void
		{
			trace("Error loading file : " + e.text);
		}




		// Save 
		public function doSave(textData:String):void
		{
			//create the FileReference instance
			fr = new FileReference();

			//listen for the file has been saved
			fr.addEventListener(Event.COMPLETE, onFileSave);

			//listen for when then cancel out of the save dialog
			fr.addEventListener(Event.CANCEL,onSaveCancel);

			//listen for any errors that occur while writing the file
			fr.addEventListener(IOErrorEvent.IO_ERROR, onSaveError);

			//open a native save file dialog, using the default file name
			fr.save(textData, DEFAULT_FILE_NAME);
		}

		/***** File Save Event Handlers ******/

		//called once the file has been saved
		private function onFileSave(e:Event):void
		{
			trace("File Saved");
			fr = null;
		}

		//called if the user cancels out of the file save dialog
		private function onSaveCancel(e:Event):void
		{
			trace("File save select canceled.");
			fr = null;
		}

		//called if an error occurs while saving the file
		private function onSaveError(e:IOErrorEvent):void
		{
			trace("Error Saving File : " + e.text);
			fr = null;
		}

	}
}